<template>
  <div class="app-container">
    <el-card shadow="never">
      <div slot="header" class="clearfix">
        <span>用户详情</span>
      </div>

      <el-form v-if="form" ref="productForm" :model="form" v-loading="loading" label-width="85px" class="uinfo-show">
        <el-form-item label="用户名称：">
            {{ form.name }}
        </el-form-item>
        <el-form-item label="真实名称：">
            {{ form.realname }}
        </el-form-item>

        <el-form-item label="个人验证">
              <el-tag
              :type="[form.check, 'class'] | checkFilter"
              close-transition>{{ [form.check, 'text'] | checkFilter }}</el-tag>
              <el-button 
              @click="dialogVisible = true"
              type="primary" 
              size="mini">审核</el-button>
        </el-form-item>

        <el-form-item label="企业认证">
            {{ form.realname }}
        </el-form-item>
      </el-form>
    </el-card>

    <el-dialog
      title="个人审核"
      :visible.sync="dialogVisible"
      width="30%">
      <el-form v-if="form" :model="form" v-loading="loading" label-width="85px">
        <el-form-item label="真实名称：">
            {{ form.realname }}
        </el-form-item>

        <el-form-item label="身份证号：">
            {{ form.idcard }}
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="success" @click="pass">通过</el-button>
        <el-button type="danger" @click="unPass">不通过</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { show, checkPass } from '@/api/test'

export default {
  data() {
    return {
      id: this.$route.params.id,
      loading: true,
      form: null,
      dialogVisible: false
    }
  },
  created() {
    this.getUserInfo()
  },
  methods: {
    getUserInfo() {
      this.loading = true
      show(this.id).then(res => {
        this.form = res.data
        this.loading = false
      })
    },
    // 审核通过
    pass() {
      checkPass(this.form.id, {
        check: 2
      }).then(res => {
        this.$message({
          message: '审核通过了',
          type: res.status === 'success' ? 'success' : 'warning'
        })
        this.getUserInfo() // 重新刷新用户数据
      })
      this.dialogVisible = false // 关闭弹窗
    },
    unPass() {
      this.dialogVisible = false
      this.$message({
        message: '审核不通过了',
        type: 'success'
      })
    }
  },
  filters: {
    checkFilter([check, type]) {
      const checkMap = {
        '-1': {
          text: '验证未通过',
          class: 'danger'
        },
        '0': {
          text: '未验证',
          class: 'warning'
        },
        '1': {
          text: '审核中',
          class: 'warning'
        },
        '2': {
          text: '已验证',
          class: 'success'
        }
      }

      if (typeof checkMap[check] !== 'undefined') {
        return checkMap[check][type]
      } else {
        return check
      }
    }
  }
}
</script>

